package yt.kpool.activity.personal;


import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.EditText;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.MainActivity;
import yt.kpool.R;
import yt.kpool.activity.salesman.SalesmanMainActivity;
import yt.kpool.activity.setting.agent.myguest.GuestChildAountActivity;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.LoginBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 登录
 */
public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.edit_phone)
    EditText edit_phone;
    @BindView(R.id.edit_pwd)
    EditText edit_pwd;

    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this, 0);

        SPUtil spUtil = new SPUtil(this, BaseApplication.UserLogin);
        String tel = spUtil.getString(BaseApplication.TEL, "");
        String password = spUtil.getString(BaseApplication.PASSWORD, "");
        edit_phone.setText(tel);
//        edit_pwd.setText(password);
    }

    /**
     * 点击事件的处理
     *
     * @param view
     */
    @OnClick({R.id.btn_login, R.id.ly_findpwd, R.id.ll_fastlook, R.id.title_left, R.id.title_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login://登录
                if (check())
                    login();
                break;
            case R.id.title_left://返回键
                finish();
                break;
            case R.id.ly_findpwd:
                //忘记密码
                startActivity(new Intent(getApplicationContext(), ForgetPwdActivity.class));
                break;
            case R.id.ll_fastlook:
                //快速查看
                startActivityForResult(new Intent(LoginActivity.this, FastLookActivity.class), 12);
                break;
            case R.id.title_right:
                //注册
                startActivity(new Intent(getApplicationContext(), RegisterActivity.class));
                break;
        }
    }

    private void login() {
        loadingDialog.show();
        NetHelperNew.login(getPhone(), getPwd(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                Log.i("-->","登录返回"+data);
                loadingDialog.dismiss();

                final LoginBean loginBean = MyGson.getInstance().fromJson(data, LoginBean.class);

                if (1 == loginBean.getType()) {
                    if (loginBean.getData().getUserType()==5)
                    {
                        save(loginBean);
                        //业务员
                        Intent intent = new Intent(LoginActivity.this, SalesmanMainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);

                    }else {
                        Intent intent = getIntent();
                        if (1 == intent.getIntExtra("type", 0)) {
                            //从起始页来的
                            setResult(10, intent);
                        }
                        save(loginBean);
                        finish();
                    }


                } else {
                    new ErrorDialog(LoginActivity.this, loginBean.getContent()).show();

                }
            }

            @Override
            public void onError(String msg) {
                loadingDialog.dismiss();
                super.onError(msg);
                Log.i("-->", "" + msg);
                new ErrorDialog(LoginActivity.this, msg).show();
//                ToastUtil.showToast(getApplicationContext(), msg);
            }
        });
    }

    /**
     * 保存数据
     *
     * @param loginBean
     */
    private void save(LoginBean loginBean) {
        SPUtil spUtil = new SPUtil(BaseApplication.context, BaseApplication.FILENAME);

        spUtil.putString(BaseApplication.NAME, loginBean.getData().getUserName());
        spUtil.putString(BaseApplication.ID, loginBean.getData().getUserID() + "");
        SPUtil spUtil2 = new SPUtil(BaseApplication.context, BaseApplication.UserLogin);
        spUtil2.putString(BaseApplication.TEL, getPhone());
        spUtil.putInt(BaseApplication.UserType, loginBean.getData().getUserType());
    }

    /**
     * 获取账号
     *
     * @return
     */
    private String getPhone() {
        return edit_phone.getText().toString().trim();
    }

    /**
     * 获取密码
     *
     * @return
     */
    private String getPwd() {
        return edit_pwd.getText().toString().trim();
    }

    /**
     * 登录之前的检测
     *
     * @return
     */
    private boolean check() {
        if (getPhone().length() != 11) {
            ToastUtil.showToast(getApplicationContext(), getString(R.string.error_tel));
            return false;
        }
        if (getPwd().length() == 0) {
            ToastUtil.showToast(getApplicationContext(), getString(R.string.error_pwd));
            return false;
        }
        if (!NetWorkUtil.checkNet(getApplicationContext())) {
            ToastUtil.showToast(getApplicationContext(), getString(R.string.error_net));
            return false;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 66) {

            setResult(66,getIntent());
            finish();
        }
    }
}